package com.compomics.prioritizer.general.gui;

import com.compomics.prioritizer.db.accessors.Instrument;
import com.compomics.prioritizer.db.accessors.User;
import com.compomics.prioritizer.db.accessors.Project;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.sql.Connection;
import java.sql.SQLException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.*;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 31-okt-2008
 * Time: 11:44:50
 * To change this template use File | Settings | File Templates.
 */
public class AcceptProjectGui extends JFrame {
    private JPanel jpanContent;
    private JTextField txtTitle;
    private JTextArea txtDescription;
    private JTextField txtUser;
    private JTextField txtInstrument;
    private JComboBox cmbInstrument;
    private JButton saveProjectButton;
    private JComboBox cmbProject;
    private JLabel txtInstrumentAccepted;


    //the connection
    private Connection iConn;
    //the Instruments
    private Instrument iInstruments[];
    //the projects
    private Project iProjects[];
    //the selected project
    private Project iProject;
    /*
    * The user role
    * 1 =  normal user
    * 2 =  user that may change priorities
    * 3 =  operator
    */
    public int iRole = -1;

    public AcceptProjectGui(Connection aConn, int aRole) {
        super("Accept new project");
        this.iConn = aConn;
        this.iRole = aRole;


        $$$setupUI$$$();
        cmbProject.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    iProject = (Project) e.getItem();
                    stateChangedProject(iProject);
                }
            }
        });
        saveProjectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveProject();
            }
        });
        if (iRole != 2) {
            saveProjectButton.setEnabled(false);
        }
        stateChangedProject((Project) cmbProject.getSelectedItem());
        setContentPane(jpanContent);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screen.width / 5), (screen.height / 10));
        this.setSize(600, 350);
        this.setVisible(true);
    }

    public void gatherData() {
        this.findInstruments();
        this.findProjects();
    }

    /**
     * This method collects all instruments.
     * It fills the 'iInstruments' cache.
     */
    private void findInstruments() {
        try {
            iInstruments = Instrument.getAllInstruments(iConn);
        } catch (SQLException sqle) {
            JOptionPane.showMessageDialog(this, new String[]{"Unable to find instruments:  ", sqle.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * This method collects all instruments.
     * It fills the 'iInstruments' cache.
     */
    private void findProjects() {
        try {
            iProjects = Project.getAllNewProjects(iConn);
            if (iProjects.length == 0) {
                JOptionPane.showMessageDialog(this, "No new projects found!", "No projects found", JOptionPane.INFORMATION_MESSAGE);
                this.close();
            }
        } catch (SQLException sqle) {
            JOptionPane.showMessageDialog(this, new String[]{"Unable to find instruments:  ", sqle.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void saveProject() {
        Instrument instrument = (Instrument) cmbInstrument.getSelectedItem();
        try {
            if (iProject != null) {
                int priority = 1 + instrument.getLowestPriority(iConn);
                long instrumentId = instrument.getInstrumentid();
                iProject.setPriority(priority);
                iProject.setL_instrumentid_accepted(instrumentId);
                iProject.updateAcceptedProject(iConn);
                JOptionPane.showMessageDialog(this, "You succesfully update a project", "Project updated", JOptionPane.INFORMATION_MESSAGE);
                AcceptProjectGui accept = new AcceptProjectGui(iConn, iRole);
                this.close();
            } else {
                JOptionPane.showMessageDialog(this, "No or an old project is selected, could not save this project!", "Project not saved", JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * This method is called whenever the user selects another element in the
     * project combobox (cmbProject).
     *
     * @param aProject Project that was selected in the combobox.
     */
    private void stateChangedProject(Project aProject) {
        try {
            if (aProject != null) {
                iProject = aProject;
                txtTitle.setText(aProject.getName());
                txtDescription.setText(aProject.getDescription());
                long instrumentid = aProject.getL_instrumentid_proposed();
                Instrument lInstrument = Instrument.getInstrumentById(iConn, instrumentid);
                txtInstrument.setText(lInstrument.getName());
                long userid = aProject.getL_userid();
                User lUser = User.getUserById(iConn, userid);
                txtUser.setText(lUser.getName());
            }
        } catch (SQLException sqle) {
            JOptionPane.showMessageDialog(this, new String[]{"Unable to find projects information:  ", sqle.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
        }

    }

    private void createUIComponents() {
        this.gatherData();
        cmbInstrument = new JComboBox(iInstruments);
        cmbProject = new JComboBox(iProjects);
    }

    public void close() {
        this.setVisible(false);
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        jpanContent = new JPanel();
        jpanContent.setLayout(new GridBagLayout());
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        jpanContent.add(panel1, gbc);
        panel1.setBorder(BorderFactory.createTitledBorder(null, "Project parameter", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.ABOVE_TOP, new Font(panel1.getFont().getName(), panel1.getFont().getStyle(), 16)));
        final JLabel label1 = new JLabel();
        label1.setText("Project title:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        panel1.add(label1, gbc);
        final JLabel label2 = new JLabel();
        label2.setText("Project description:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        panel1.add(label2, gbc);
        final JLabel label3 = new JLabel();
        label3.setText("User:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        panel1.add(label3, gbc);
        final JLabel label4 = new JLabel();
        label4.setText("Instrument proposed:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        panel1.add(label4, gbc);
        final JLabel label5 = new JLabel();
        label5.setText("Instrument accepted:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        panel1.add(label5, gbc);
        final JScrollPane scrollPane1 = new JScrollPane();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        panel1.add(scrollPane1, gbc);
        txtDescription = new JTextArea();
        txtDescription.setRows(5);
        scrollPane1.setViewportView(txtDescription);
        txtTitle = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel1.add(txtTitle, gbc);
        txtUser = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel1.add(txtUser, gbc);
        txtInstrument = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel1.add(txtInstrument, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel1.add(cmbInstrument, gbc);
        saveProjectButton = new JButton();
        saveProjectButton.setText("Save project");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(saveProjectButton, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 0, 5, 0);
        jpanContent.add(cmbProject, gbc);
        final JLabel label6 = new JLabel();
        label6.setText("Selec a project:         ");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        jpanContent.add(label6, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return jpanContent;
    }
}
